package com.salon.common.http.response;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 请求响应对象
 **/

@Data
@ApiModel("请求响应对象")
@EqualsAndHashCode(callSuper = true)
public class ObjectResult<T> extends BaseResult {

    @ApiModelProperty(value = "响应数据")
    private T data;

    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public static <T> ObjectResult<T> success(T data, String message) {
        ObjectResult<T> result = new ObjectResult<>();
        result.setCode(ResultCode.SUCCESS);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    public static <T> ObjectResult<T> success(T data) {
        ObjectResult<T> result = new ObjectResult<>();
        result.setCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    public static ObjectResult<String> success() {
        ObjectResult<String> result = new ObjectResult<>();
        result.setCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 无数据失败返回
     *
     * @param code    返回码
     * @param message 消息
     * @return ignore
     */
    public static <T> ObjectResult<T> failed(ResultCode code, String message) {
        ObjectResult<T> result = new ObjectResult<>();
        result.setCode(code);
        result.setData(null);
        result.setMessage(message);
        return result;
    }

    /**
     * @param code    编码
     * @param data    数据
     * @param message 消息
     * @param <T>     返回值
     * @return ignore
     */
    public static <T> ObjectResult<T> failed(ResultCode code, T data, String message) {
        ObjectResult<T> result = new ObjectResult<>();
        result.setCode(code);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

}
